<?php
/**
 * 功能: 订单管理
 * User: WD
 * Date: 2018/10/19 0019
 * Time: 11:11
 */

namespace app\admin\model;
use think\Db;
use think\Model;

class GoodsOrder extends Model{
    /**
     *  订单搜索
     * @param $change
     * @param $logmax
     * @param $logmin
     * @param $state
     * @param $common
     */
    public function searchOrder($change,$logmax,$logmin,$state,$common){
        if($change==1){
            //创建时间
            if(!empty($logmax)&& !empty($logmin)){
                $orders = Db::name('goods_order')
                    ->whereBetween('create_time',[$logmin,$logmax.' 23:59:59'])
                    ->order('id desc')
                    ->paginate(10,false,['query'=>request()->param()])->each(function($item,$key){
                        if(!empty($item['user_id'])){
                            $item['phone'] = Db::name('user')->where('id',$item['user_id'])->value('phone');

                        }else{
                            $item['phone'] = '';
                        }
                        return $item;
                    });
            }elseif(empty($logmax)&& !empty($logmin)){
                $orders = Db::name('goods_order')
                    ->where('create_time','>=',$logmin)
                    ->order('id desc')
                    ->paginate(10,false,['query'=>request()->param()])->each(function($item,$key){
                        if(!empty($item['user_id'])){
                            $item['phone'] = Db::name('user')->where('id',$item['user_id'])->value('phone');

                        }else{
                            $item['phone'] = '';
                        }
                        return $item;
                    });
            }elseif(!empty($logmax)&& empty($logmin)){
                $orders = Db::name('goods_order')
                    ->where('create_time','<=',$logmax.' 23:59:59')
                    ->order('id desc')
                    ->paginate(10,false,['query'=>request()->param()])->each(function($item,$key){
                        if(!empty($item['user_id'])){
                            $item['phone'] = Db::name('user')->where('id',$item['user_id'])->value('phone');

                        }else{
                            $item['phone'] = '';
                        }
                        return $item;
                    });
            }


        }
        elseif($change==2){
            //订单状态
            $orders = Db::name('goods_order')
                ->where('state',$state)
                ->order('id desc')
                ->paginate(10,false,['query'=>request()->param()])->each(function($item,$key){
                    if(!empty($item['user_id'])){
                        $item['phone'] = Db::name('user')->where('id',$item['user_id'])->value('phone');

                    }else{
                        $item['phone'] = '';
                    }
                    return $item;
                });

        }elseif($change==3){
            //订单号/商品详情
            $orders = Db::name('goods_order')
                ->where('order_num','like','%'.$common.'%')
                ->whereOr('info','like','%'.$common.'%')
                ->order('id desc')
                ->paginate(10,false,['query'=>request()->param()])->each(function($item,$key){
                    if(!empty($item['user_id'])){
                        $item['phone'] = Db::name('user')->where('id',$item['user_id'])->value('phone');

                    }else{
                        $item['phone'] = '';
                    }
                    return $item;
                });

        }
        elseif($change==4){
            //结算时间
            if(!empty($logmax)&& !empty($logmin)){
                $orders = Db::name('goods_order')
                    ->whereBetween('finish_time',[$logmin,$logmax.' 23:59:59'])
                    ->order('id desc')
                    ->paginate(10,false,['query'=>request()->param()])->each(function($item,$key){
                        if(!empty($item['user_id'])){
                            $item['phone'] = Db::name('user')->where('id',$item['user_id'])->value('phone');

                        }else{
                            $item['phone'] = '';
                        }
                        return $item;
                    });
            }elseif(empty($logmax)&& !empty($logmin)){
                $orders = Db::name('goods_order')
                    ->where('finish_time','>=',$logmin)
                    ->order('id desc')
                    ->paginate(10,false,['query'=>request()->param()])->each(function($item,$key){
                        if(!empty($item['user_id'])){
                            $item['phone'] = Db::name('user')->where('id',$item['user_id'])->value('phone');

                        }else{
                            $item['phone'] = '';
                        }
                        return $item;
                    });
            }elseif(!empty($logmax)&& empty($logmin)){
                $orders = Db::name('goods_order')
                    ->where('finish_time','<=',$logmax.' 23:59:59')
                    ->order('id desc')
                    ->paginate(10,false,['query'=>request()->param()])->each(function($item,$key){
                        if(!empty($item['user_id'])){
                            $item['phone'] = Db::name('user')->where('id',$item['user_id'])->value('phone');

                        }else{
                            $item['phone'] = '';
                        }
                        return $item;
                    });
            }
        }
        return $orders;

    }

    public function searchcount($change,$logmax,$logmin,$state,$common){
        if($change==1){
            //创建时间
            if(!empty($logmax)&& !empty($logmin)){
                $count = Db::name('goods_order')
                    ->whereBetween('create_time',[$logmin,$logmax.' 23:59:59'])
                    ->count();
            }elseif(empty($logmax)&& !empty($logmin)){
                $count = Db::name('goods_order')
                    ->where('create_time','>=',$logmin)
                   ->count();
            }elseif(!empty($logmax)&& empty($logmin)){
                $count = Db::name('goods_order')
                    ->where('create_time','<=',$logmax.' 23:59:59')
                    ->count();
            }


        }
        elseif($change==2){
            //订单状态
            $count = Db::name('goods_order')
                ->where('state',$state)
                ->count();

        }elseif($change==3){
            //订单号/商品详情
            $count = Db::name('goods_order')
                ->where('order_num','like','%'.$common.'%')
                ->whereOr('info','like','%'.$common.'%')
                ->count();

        }
        elseif($change==4){
            //结算时间
            if(!empty($logmax)&& !empty($logmin)){
                $count = Db::name('goods_order')
                    ->whereBetween('finish_time',[$logmin,$logmax.' 23:59:59'])
                    ->count();
            }elseif(empty($logmax)&& !empty($logmin)){
                $count = Db::name('goods_order')
                    ->where('finish_time','>=',$logmin)
                    ->count();
            }elseif(!empty($logmax)&& empty($logmin)){
                $count = Db::name('goods_order')
                    ->where('finish_time','<=',$logmax.' 23:59:59')
                    ->count();
            }
        }
        return $count;
    }

}